package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ChatGroup;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by shijunting on 15-1-26.
 */
public class GetChatGroupMsg extends CcMsgStructure {

    private static final String TAG = "GetChatGroupMsg";

    private Message mmCallback;
    private Context mContext;

    public GetChatGroupMsg(Message callback ,Context mContext) {
        mmCallback = callback;
        mContext=mContext;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            ChatGroup chatGroup = new ChatGroup();
            if(jsonObject.has(AsynHttpClient.KEY_CHAT_GROUP_ID)){
                chatGroup.id = jsonObject.getString(AsynHttpClient.KEY_CHAT_GROUP_ID);
            }
            if(jsonObject.has(AsynHttpClient.KEY_CHAT_GROUP_IMG)){
                chatGroup.img=jsonObject.getString(AsynHttpClient.KEY_CHAT_GROUP_IMG);
            }
            if(jsonObject.has(AsynHttpClient.KEY_CHAT_GROUP_NAME)){
                chatGroup.name=jsonObject.getString(AsynHttpClient.KEY_CHAT_GROUP_NAME);
            }
            if (jsonObject.has("ctime")) {
                chatGroup.lastUpdateTime = jsonObject.getString("ctime");
            }
            if(jsonObject.has("count")){
                chatGroup.memberCount=jsonObject.getString("count");
            }
            ArrayList<String> nameCard = (ArrayList<String>) PreferenceUtil.getInstance(mContext).getCloudContact(Constants.CONTACT_NAME_CARD);
            chatGroup.lastMsgName=nameCard.get(0);
            chatGroup.lastMsgContent="发起了聊天";
            chatGroup.userType="2";

            if (mmCallback != null) {
                if("".equals(chatGroup.getId())||chatGroup.getId()==null){
                    mmCallback.arg1=1;
                }else{
                    mmCallback.obj = chatGroup;
                    mmCallback.arg1 = 0;
                }
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
